var MOD_TFALL = {
  printhook:printtfallview
}


//--------------------------------------------------------------------------------------

function tfall_onclickondoc(cdoc) {
  edit_features(cdoc);
}

function tfall_onclickonclaim(cl) {
  filter_doselect("List",cl,"edit_features()");
}



//--------------------------------------------------------------------------------------

function tfall_tffindings(cl,tf,tfeq,option) {
  var html="";
  var s_x=0,s_a=0,p_x=0,p_a=0,h_x=0,h_a=0;
  var doc_iter = new doc_iterator();
  var star_img= tfeq_img;
  var opin_img= tfeq_img;
  var strat_img;
  
  var clfound=0; // passage found
  var clneed =0; // passages needed (depends on hints) 

  if(tf) {
    while(!doc_iter.end()) { 
      if(!doc_iter.isNotCited()) { 
	var pn=doc_iter.getPN();
	var hint=GetPassageHintEq(cl,tf,pn);                
	var pass=GetPassageReferenceEq(cl,tf,pn);
	if(hint) { // take  hint even if pass
	  if(is_present_hint(hint)) {
	    clfound+=2;
	  } else {
	    clneed= Math.max(clneed,getHintNeeds(hint));
	  }
	} else if(pass) {
	  clfound+=2;
	}       
	
	if(GetClaimDoc_X(cl)==pn){
	  // X document 
	  s_x=1;
	  if(pass) {
	    p_x=1;
	  } else if(hint) {
	    if(is_present_hint(hint)) {
	      p_x=1;
	    } else {
	      h_x=1;
	    }
	  }
	} else if(GetClaimDoc_Y(cl)==pn || GetClaimDoc_A(cl)==pn || GetClaimDoc_B(cl)==pn){
	  if(pass) {
	    p_a=1;
	  } else if(hint) {
	    if(is_present_hint(hint)) {
	      p_a=1;
	    } else {
	      h_a=1;
	    }
	  }
	  s_a=1; //alert(cl+"."+tf+" "+pn);
	}
      }     
      doc_iter.next();
    }
  
    if(clfound || clneed) {
      if(clfound>=clneed  || clneed==1) {
	star_img=tffound_img;
      } else {
	star_img=tfhalffound_img;
      }
    } else {
      star_img=tfnotfound_img;
    }
    
    if(s_x) { // not new or not inventive 
      if(p_x) {
	opin_img=notnew_img;
      } else if(h_x && p_a) {
	opin_img=notinv_img;
      } else {
	opin_img=tfnotfound_img;
      }
    } else if(s_a) { // new and inventive 
      if(p_a) {
	opin_img=notnew_img;
      } else if(h_a) {
	opin_img=notinv_img;
      } else {
	opin_img=newinv_img; // invention is here
      }
    } else {
      opin_img=notsearched_img;
    }  
  } // end of if tf


  if(IsClaimNotNovel(cl)) {
    strat_img=notnew_img;
  } else if(IsClaimNotInventive(cl)) {
    strat_img=notinv_img;
  } else if(IsClaimNovelandInventive(cl)) {
    strat_img=newinv_img;
  } else {
    strat_img=notsearched_img;
  }

  if(option) {
    html_img_replace("star"+cl+"_"+tf,star_img);
    html_img_replace("opin"+cl+"_"+tf,opin_img);
    html_img_replace("strat"+cl+"_"+tf,strat_img);
  } else { // "("+clfound+"/"+clneed+")"
    html+="<TD align=center>"+html_img(star_img,"star"+cl+"_"+tf)+"</TD>";
    html+="<TD align=center>"+html_img(opin_img,"opin"+cl+"_"+tf)+"</TD>";
    html+="<TD align=center>"+html_img(strat_img,"strat"+cl+"_"+tf)+"</TD>";
  }

  return html;
}

//--------------------------------------------------------------------------------------
function  tfall_updatenotsearchedview() {
  //document.getElementById("noop").value=List_tolist(GetAllClaims("??"));
}

//--------------------------------------------------------------------------------------

function tfall_getstratclass(cl,pn,eq) {
  switch(GetClaimDocStrat(cl,pn,eq)) {
  case "X":
  case "Y":
    return "STRATX";
  case "A":
  case "B":
    return "STRATA";
    break;
  default:
    return "STRAT0";
  }
}

//--------------------------------------------------------------------------------------

function tfall_onchangetfhint(cl,tf,pn){
  var newhintval=html_selected("hint"+cl+"_"+tf+pn);    
  var taint=get_Taint();
  featureupdatehints(cl,tf,pn,newhintval);
  tfall_updategui(cl,taint);
}



function tfall_updateclSV(cl,pn,doc) {
  html_doselect("ssel"+cl+"_"+doc,CLSTRATS,GetClaimDocStrat(cl,pn));     
  html_doselect("vsel"+cl+"_"+doc,CLVALS ,GetClaimDocVal(cl,pn));
  if(IsClaimDocValAuto(cl,pn)) {
    document.getElementById("vsel"+cl+"_"+doc).disabled=1;
  } else {
    document.getElementById("vsel"+cl+"_"+doc).disabled=0;
  }
  
  document.getElementById("sstd"+cl+"_"+doc).className=
    tfall_getstratclass(cl,pn);	  
}

//--------------------------------------------------------------------------------------

function tfall_updategui(cl,from) {
  var cl_iter,tf_iter;
  var doc_iter=new doc_iterator();
  var icl="",cltf,hint,obj,pn;
  var docx=GetClaimDoc_X(cl);
  // update bg colors 
  while(!doc_iter.end()) {
    if(!doc_iter.isNotCited()) {
      cl_iter= new PageClaim_Iterator();  //filteredclaims_iterator(cl); 
      pn=doc_iter.getPN();
      while(!cl_iter.end()) {
	icl=cl_iter.getValue();
	if(is_TaintedFrom(from,icl)) { 	
	  tfall_updateclSV(icl,doc_iter.getPN(),doc_iter.getValue());
	}
	cl_iter.next();
      }
    }
    doc_iter.next();
  }
  // update hints of X document
  if(docx){
    cl_iter= new PageClaim_Iterator();  //filteredclaims_iterator(cl); 
    while(!cl_iter.end()) {
      icl=cl_iter.getValue();      
      var tf_iter= new claim_features_iterator(icl);
      while(!tf_iter.end()) {
	hint=GetPassageHintEq(icl,tf_iter.gettf(),docx);
	html_doselect("hint"+icl+"_"+tf_iter.gettf()+docx,ALL_HINTS,hint);
	obj=document.getElementById("hinttd"+cl+"_"+tf_iter.gettf()+docx);
	if(obj) obj.className= getPassClass(tf_iter.getcl(),tf_iter.gettf(),docx); // hint ? "FOUND" : "NOTFOUND";
	tf_iter.next();
      }
      cl_iter.next();
    }
  }
  // update final findings (last col) 
  cl_iter= new PageClaim_Iterator();  //filteredclaims_iterator(cl); 
  while(!cl_iter.end()) {
    icl=cl_iter.getValue();
    if(is_TaintedFrom(from,icl)) { 	
      tf_iter= new claim_features_iterator(icl);
      while(!tf_iter.end()) { 
	tfall_tffindings(icl,tf_iter.getValue(),"","update");
	tf_iter.next();
      }
    }
    cl_iter.next();
  }
  
 
  tfall_updatenotsearchedview();
}


//--------------------------------------------------------------------------------------

function tfall_onchangestrat(cl,doc) { 
  var newstrat=html_selected("ssel"+cl+"_"+doc,CLSTRATS);
  var oldx=GetClaimDoc_X(cl);
  var taint= get_Taint();
  tf_onchangestrat(cl,doc);
  // changed X doc, big gui update, reload
  if(newstrat=="X" || oldx==doc) {
    edit_features();
  } else {  
    tfall_updategui(cl,taint);
  }
}

//--------------------------------------------------------------------------------------


function tfall_onchangeval(cl,doc) {
  var oldx=GetClaimDoc_X(cl);
  var taint= get_Taint();
  tf_onchangeval(cl,doc); // may change strategy
  if(GetClaimDoc_X(cl) != oldx) {
    edit_features();
  } else {  
    tfall_updategui(cl,taint);
  }
}



//--------------------------------------------------------------------------------------
// Docs view 
//--------------------------------------------------------------------------------------


function tfall_showfeaturesforclaims(){
  var html=new Array("<TABLE border=1><TR><TH>Docs</TH>");
  var htf=0; // hit
  var mtf=0; // miss
  var alltf=0;
  var docval= new Array();

  ACT_MOD= MOD_TFALL;

  CURRENTVIEW="tfviewall";

  var doc_iter = new doc_iterator();

  while(!doc_iter.end()) { 
    if(!doc_iter.isNotCited()) { 
      var doc=doc_iter.getValue(); 
      docval[doc]=0;
      html.push("<TH colspan=2>");
      html.push(html_whatis("<input type=button value="+
			    doc_iter.getPN()+
			    " class='FULLSIZE' onClick='tfall_onclickondoc(this.value);'>",
			    GetDocDBTitle(doc_iter.getPN())));
      html.push("</TH>");
    }
    doc_iter.next();
  }
  
  html.push("</TR>");
  var tabhtml=new Array("");
  var pcl_iter=new PageClaim_Iterator();  
  while(!pcl_iter.end()) {
    var cl=pcl_iter.getValue();
    var eq=GetClaimEq(cl);
    var clclass=ClaimTypeStyle(cl);

    tabhtml.push("<TR><TD><button CLASS='"+clclass+"' style='width:100%;margin:0 0 0 0;' "+
		 "' onClick='tfall_onclickonclaim("+cl+");'><small>Claim "+cl+" <sup>"+GetClaimDep(cl)+"</sup></small></button></TD>");
    var doc_iter = new doc_iterator();    
    var options="";
    var tfcl=cl;	
    if(eq) { // use eq claim features for calculation  (was || (eq && GetPassageReference(eq,1,docid)))
      tfcl=eq;
      options=" disabled";
    }        
    
    while(!doc_iter.end()) { 
      if(!doc_iter.isNotCited()) { 
	var pn=doc_iter.getPN();
	var doc = doc_iter.getValue();
	htf=0; mtf=0;
	var tf_iter= new claim_features_iterator(tfcl);
	while(!tf_iter.end()) { 
	  if(tf_iter.getPassageEqIgn(pn)) {
	    htf++;
	  } else {
	    mtf++;
	  }
	  tf_iter.next();
	}
	tabhtml.push("<TD align=center>"+htf+"/"+(mtf+htf)+"</TD>"); 
	tabhtml.push("<TH align=right id='sstd"+cl+"_"+doc+"' class='"+tfall_getstratclass(cl,pn,eq)+"'> S");
	tabhtml.push(html_selection_ns("ssel"+cl+"_"+doc,
				    CLSTRATS,
				    GetClaimDocStrat(cl,pn,eq),
				    "tfall_onchangestrat("+cl+","+doc+")",options));
	
	tabhtml.push(" V"+html_selection_ns("vsel"+cl+"_"+doc,
					 CLVALS,
					 GetClaimDocVal(cl,pn,eq),
					 "tfall_onchangeval("+cl+","+doc+")",
					 options || (IsClaimDocValAuto(cl,pn) && " disabled "))+"</TH>");
		     
	docval[doc]+=htf;
      }
      doc_iter.next();
    }
    alltf+=(mtf+htf);
    
    tabhtml.push("<TH>*</TH><TH>O</TH><TH>S</TH>");  
    tabhtml.push("</TR>"); 
    
    if(eq) { 
      tabhtml.push("<TR><TD class=eqtf align=center> TF "+cl+".x</TD>");
      var doc_iter = new doc_iterator();
      while(!doc_iter.end()) { 
	if(!doc_iter.isNotCited()) { 
	  tabhtml.push("<TD colspan=2 align=center class=eqtf>"+eq+".x</TD>");
	}
	doc_iter.next();
      }	
      tabhtml.push(tfall_tffindings(cl));
      tabhtml.push("</TR>");
    } else {
      var tf_iter= new claim_features_iterator(cl);      
      while(!tf_iter.end()) { 
	var clfound=0; // passage found
	var clneed =0; // passages needed (depends on hints) 
	var tfeq=tf_iter.getEq();
	var eqclass="";
	if(tfeq && tfeq!="")  {    
	  eqclass=" class='eqtf' ";
	}
	tabhtml.push("<TR><TD "+eqclass+" align=center>"+
		     html_whatis("TF "+cl+"."+tf_iter.getValue(),tf_iter.getDesc())+"</TD>");
	var doc_iter = new doc_iterator();
	while(!doc_iter.end()) { 
	  if(!doc_iter.isNotCited()) { 
	    var pn=doc_iter.getPN();
	    var hint=tf_iter.getHintEq(pn);                
	    var pass=tf_iter.getPassageEqIgn(pn);
	    var fclass=getPassClass(tf_iter.getcl(),tf_iter.gettf(),pn);
	    if(GetClaimDoc_X(cl)==pn) {
	      /* X document */
	      if(is_glue_hint(hint)) {
		tabhtml.push("<TD colspan=2 align=center class='"+fclass+"'>"+GLUE_HINTS[0]+"</TD>");		
	      } else if(!pass) {
		tabhtml.push("<TD align=center  class=FOUND id='hinttd"+cl+"_"+tf_iter.gettf()+pn+
			     "'>H</TD><TD align=center class='"+fclass+"'>"+
			     html_selection("hint"+cl+"_"+tf_iter.gettf()+pn,ALL_HINTS,hint			      ,
					    "tfall_onchangetfhint("+cl+","+tf_iter.gettf()+",\""+pn+"\");",
					    "class='FULLW' "+(tf_iter.getEq() ? " disabled" : ""))+"</TD>");
	      } else {
		tabhtml.push("<TD colspan=2 align=center class='"+fclass+"'>"+html_whatis((tfeq || "*"),
											  pass)+"</TD>");
	      }
	    } else {
	      /* Another document */
	      if(hint) { // take  hint even if pass
		tabhtml.push("<TD colspan=2 align=center class='"+fclass+"'>"+html_whatis((tfeq || hint),
										   hint)+"</TD>");
	      } else if(pass) {
		tabhtml.push("<TD colspan=2 align=center class='"+fclass+"'>"+html_whatis((tfeq || "*"),
										   pass)+"</TD>");
	      } else {
		tabhtml.push("<TD colspan=2 align=center class='"+fclass+"'>"+(tfeq || "")+"</TD>");
	      } 
	    }
	  }             
	  doc_iter.next();	  
	}
	tabhtml.push(tfall_tffindings(cl,tf_iter.getValue(),tfeq));	
	tabhtml.push("</TR>");
	tf_iter.next();
      }
    }
    pcl_iter.next();
  }
  
  tabhtml.push("</TABLE>");
  html.push("<TR><TH>Claims</TH>");
  var doc_iter = new doc_iterator();
  while(!doc_iter.end()) { 
    if(!doc_iter.isNotCited()) {
      html.push("<TH colspan=2>"+docval[doc_iter.getValue()]+"/"+alltf+"</TH>");
    }
    doc_iter.next();
  }
  html.push("</TR>");
  return html.concat(tabhtml);
}


function printtfallview() {
  var doc_iter = new doc_iterator();
  var i=1;
  var html="<small>";
  codeprt();
  while(!doc_iter.end()) { 
    if(!doc_iter.isNotCited()) { 
      html+="D"+(i++)+": "+doc_iter.getPN()+BR;
    }
    doc_iter.next();
  }
  html+="<BR>&nbsp<BR>";
  html+="</small><TABLE border=1 style='border-collapse: collapse;padding: 5pt;'>";
  var cl_iter =  new PageClaim_Iterator();  // filteredclaims_iterator();
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    html+="<TR><TH VALIGN=top>"+cl;
    html+=". </TH><TD align=left>";
    if(GetClaimEq(cl)) {
      html+="="+GetClaimEq(cl);
    }
    html+="</TD>";

    i=1;
    doc_iter = new doc_iterator();
    while(!doc_iter.end()) { 
      if(!doc_iter.isNotCited()) { 
	html+="<TH>D"+(i++)+"</TH>";
      }
      doc_iter.next();
    }
    html+="</TR><TR>";
    var tf_iter= new claim_features_iterator(cl);
    while(!tf_iter.end()) {
      html+="<TD align=center><font size=-2>"+tf_iter.gettext();
      if(tf_iter.getEq()) {
	html+="<BR>("+tf_iter.getEq()+")";
      }
      html+="</font></TD>";
      html+="<TD><font size=-3>"; 
      switch(tf_iter.getUnclarity()) {
      case "added":
	html+="<u>"+tf_iter.getDesc()+"</u>";
	break;
      case "":
	html+=tf_iter.getDesc();
	break;
      default:
	html+="<i>"+tf_iter.getDesc()+"</i>";
	break;
      }
      html+="</font></TD>"; 
      doc_iter = new doc_iterator();
      while(!doc_iter.end()) { 
	if(!doc_iter.isNotCited()) { 
	  var pn=doc_iter.getPN();
	  var hint=tf_iter.getHintEq(pn);                
	  var pass=tf_iter.getPassageEq(pn);	  
	  html+="<TD align=center><font size=-2>"+(pass || hint || "&nbsp")+"</font></TD>";
	}
	doc_iter.next();
      }
      html+="</TR><TR>";
      tf_iter.next();
    }   
    html +="</TR>";
    cl_iter.next();
  }
  html+="</TABLE>";
  return html;
}
